using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroMsg.Manager
{
	public class TempCacheMgr
	{
		private const int MAX_TEMP_FILE_NUM = 30;
		private const int RESERVE_TEMP_FILE_NUM = 10;
		private static StorageHash hasher;
		private static int int_0;
		static TempCacheMgr()
		{
			TempCacheMgr.hasher = new StorageHash();
			TempCacheMgr.int_0 = -1;
			AccountMgr.registerLoginNotify(new onAccountLoginCallback(TempCacheMgr.onLogin));
			if (AccountMgr.isLogin)
			{
				TempCacheMgr.onLogin();
			}
		}
		private static void onLogin()
		{
			TempCacheMgr.hasher.setParam(1, StorageIO.getTempPath());
			TempCacheMgr.int_0 = -1;
		}
		public static bool write(string string_0, byte[] data_buffer)
		{
			if (!string.IsNullOrEmpty(string_0) && data_buffer != null && data_buffer.Length > 0)
			{
				TempCacheMgr.tryClear();
				return TempCacheMgr.hasher.write(string_0, new byte[][]
				{
					data_buffer
				});
			}
			return false;
		}
		public static byte[] read(string string_0)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return null;
			}
			return TempCacheMgr.hasher.read(string_0);
		}
		public static void tryClear()
		{
			if (TempCacheMgr.int_0 <= 0)
			{
				TempCacheMgr.int_0 = StorageIO.filesNumber(StorageIO.getTempPath());
			}
			if (TempCacheMgr.int_0 < 30)
			{
				return;
			}
			List<FileDetail> list = StorageIO.filesDetail(StorageIO.getTempPath());
			if (list == null)
			{
				return;
			}
			list = Enumerable.ToList<FileDetail>(Enumerable.Take<FileDetail>(Enumerable.OrderByDescending<FileDetail, long>(list, (FileDetail fileDetail_0) => fileDetail_0.idleMinutes), list.get_Count() - 10));
			if (list.get_Count() < 0)
			{
				return;
			}
			using (List<FileDetail>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FileDetail current = enumerator.get_Current();
					StorageIO.deleteFile(current.strFileName);
				}
			}
			TempCacheMgr.int_0 = 10;
		}
	}
}
